<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('新增Request')" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: datetimepicker-css" />
    <style type="text/css">
        table label.error{position: inherit;}select + label.error{z-index:1;right:40px;}
    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-request-add">
        <h4 class="form-header h4">Request信息</h4>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">名称：</label>
                    <div class="col-sm-8">
                        <input id="requestName" name="requestName" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">描述：</label>
                    <div class="col-sm-8">
                        <input name="description" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </div>

        <h4 class="form-header h4">Sample信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <button type="button" class="btn btn-success btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                <button type="button" class="btn btn-danger btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>
<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: select2-js" />
<script th:src="@{/js/jquery.tmpl.js}"></script>
<script th:inline="javascript">
    $(function() {
        // 初始化数据, 可以由后台传过来
        var data = [
            {
                sampleId: "100",
                sampleName: "",
                slot: "1",
                solvent: "13",
                quality: "50.5",
                description: "",
                mFormula: "C3H7NO2",
                mWeight: "89.0932",
                servicestemp: "",
            },
            {
                sampleId: "100",
                sampleName: "",
                slot: "2",
                solvent: "13",
                quality: "60.6",
                description: "",
                mFormula: "C6H14N4O2",
                mWeight: "174.201",
                servicestemp: "",
            }];
        var options = {
            data: data,
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [{
                checkbox: true
            },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                        var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                        var columnId = $.common.sprintf("<input type='hidden' name='samples[%s].sampleId' value='%s'>", index, row.id);
                        return columnIndex + $.table.serialNumber(index) + columnId;
                    }
                },
                {
                    field: 'sampleName',
                    align: 'center',
                    title: '样品名称<span style="color: red">*</span>',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control sampleName' type='text' name='samples[%s].sampleName' value='%s' required>", index, value);
                        return html;
                    }
                },
                {
                    field: 'slot',
                    align: 'center',
                    title: 'slot<span style="color: red">*</span>',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control position' type='text' name='samples[%s].slot' value='%s' required>", index, value);
                        return html;
                    }
                },
                {
                    field: 'solvent',
                    align: 'center',
                    title: '溶剂',
                    formatter: function(value, row, index) {
                        var solvents = [[${solvents}]];
                        var data = [{ index: index, type: value , solvents: solvents}];
                        return $("#solventTpl").tmpl(data).html();
                    }
                },
                {
                    field: 'quality',
                    align: 'center',
                    title: '样品量',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='samples[%s].quality' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'description',
                    align: 'center',
                    title: '描述',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='samples[%s].description' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'mFormula',
                    align: 'center',
                    title: '分子式',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='samples[%s].mFormula' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'mWeight',
                    align: 'center',
                    title: '分子量',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='samples[%s].mWeight' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'servicestemp',
                    align: 'center',
                    title: '服务<span style="color: red">*</span>',
                    width: 350,
                    formatter: function(value, row, index) {
                        //弹窗
                        var html = $.common.sprintf("<input type='text' readonly id='input-select%s' class='form-control select2-multiple' onclick='selectServices(%s)' value='%s'>",index, index, value);
                        var hiddenHtml = $.common.sprintf("<input type='text' hidden='hidden' class='svtype_ids' id='input-h-select%s' name='samples[%s].servicestemp'>",index,index);
                        return html + hiddenHtml;
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
        };
        $.table.init(options);
    });

    var prefix = ctx + "system/request"
    $("#form-request-add").validate({
        focusCleanup: true,
        onkeyup: false,
        rules:{
            requestName:{
                minlength: 5,
                maxlength: 150,
                remote: {
                    url: prefix + "/checkRequestNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "requestName": function () {
                            return $.common.trim($("#requestName").val());
                        }
                    }
                }
            }
        },
        messages:{
            "requestName": {
                remote: "Request已经存在"
            }
        }
    });

    // $.validator.addMethod("sampleName", function(value, element) {
    //     return !this.optional(element);
    // }, "样品名称必填。");

    $.validator.addMethod("position", function(value, element) {
        return !this.optional(element) && (value <= 24 && value >= 1);
    }, "position区间1-24。");

    /* 主子表-提交 */
    function submitHandler(index, layero){

        if ($.validate.form()) {
            var ids = $('.svtype_ids').val();
            if(ids.length == 0){
                $.modal.alertWarning("请至少选择一个服务");
                return;
            }
            $.operate.passwordCheck("Enter your password", prefix + "/signature", function () {
                var data = $("#form-request-add").serializeArray();
                alert(JSON.stringify(data))
                $.operate.saveModal(prefix + "/add", data);
            })
        }
    }

    function addRow() {
        var count = $("#" + table.options.id).bootstrapTable('getData').length;
        var row = {
            index: $.table.serialNumber(count),
            sampleName: "",
            slot: "",
            solvent: "",
            quality: "",
            description: "",
            mFormula: "",
            mWeight: "",
            servicestemp: "",
        }
        sub.addRow(row);
    }

    function selectServices(num){
        var options = {
            title: 'Select Services',
            url: prefix + "/selServices",
            callBack: doSubmit2
        };
        $.modal.openOptions(options);

        function doSubmit2(index, layero){
            var rows = layero.find("iframe")[0].contentWindow.getSelections();
            var ids = layero.find("iframe")[0].contentWindow.getIdSelections();
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一个服务");
                return;
            }
            $('#input-select'+num).attr('value', rows.join())
            $('#input-h-select'+num).attr('value', ids.join())
            $.modal.close(index);
        }
    }


</script>
</body>
</html>

<script id="solventTpl" type="text/x-jquery-tmpl">
    <div>
    <select class='form-control' name='samples[${index}].solvent'>
    <option value=""></option>
    {{each(i,solvent) solvents}}
        <option value="${solvent.solventName}" {{if type == solvent.solventId}}selected{{/if}}>{{= solvent.solventName}}</option>
    {{/each}}
</select>
</div>
</script>